#/*******************************************************************
# * This file is part of the Emulex Linux Device Driver for         *
# * Fibre Channel Host Bus Adapters.                                *
# * Copyright (C) 2003-2006 Emulex.  All rights reserved.           *
# * EMULEX and SLI are trademarks of Emulex.                        *
# * www.emulex.com                                                  *
# *                                                                 *
# * This program is free software; you can redistribute it and/or   *
# * modify it under the terms of version 2 of the GNU General       *
# * Public License as published by the Free Software Foundation.    *
# * This program is distributed in the hope that it will be useful. *
# * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
# * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
# * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
# * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
# * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
# * more details, a copy of which can be found in the file COPYING  *
# * included with this package.                                     *
# *******************************************************************/
# This Makefile builds the lpfcdfc module for 2.6 Linux kernels.
######################################################################

ifeq (,$(KERNELVERSION))
   KERNELVERSION := $(shell uname -r)
endif

ifeq (,$(STANDALONE))
   STANDALONE := 1
endif

ifeq (,$(BASEINCLUDE))
   BASEINCLUDE := /lib/modules/$(KERNELVERSION)/build
endif

ifeq ($(shell uname -m),ppc64)
ifndef ($(CROSS_COMPILE))
CROSS_COMPILE := $(shell if [ -e /usr/local/ppc64-current3.0/bin/powerpc64-linux-gcc ]; then \
    echo /usr/local/ppc64-current3.0/bin/powerpc64-linux-; \
    else if [ -e /opt/cross/bin/powerpc64-linux-gcc ]; then \
    echo /opt/cross/bin/powerpc64-linux- ; fi fi)
endif
endif

# KERNELRELEASE is in the second pass of the 2.6 make process
ifneq (,$(KERNELRELEASE))

obj-$(CONFIG_SCSI_LPFC_IOCTLS) := lpfcdfc.o

lpfcdfc-objs := lpfc_cdev.o \
		lpfc_util_ioctl.o \
		lpfc_hbaapi_ioctl.o \
		lpfc_debug_ioctl.o \
		lpfc_misc.o \
		lpfc_hbadisc.o \
		lpfc_els.o \
		lpfc_ct.o \
		lpfc_mbox.o \
		lpfc_mem.o \
		lpfc_nportdisc.o \
		lpfc_sli.o \
		lpfc_scsi.o \
		lpfc_attr.o \
		lpfc_init.o

ifeq (,$(STANDALONE))
EXTRA_CFLAGS += -I$(SUBDIRS)/../kernel/lpfc

#MOD1VERDIR = $(subst ioctls/,,$(MODVERDIR))

#FORCE: $(MODVERDIR)/lpfc.mod

#$(MODVERDIR)/lpfc.mod: $(MOD1VERDIR)/lpfc.mod
#	cp $^ $@
endif

else

# Building for 2.6 kernel

ifeq (,$(STANDALONE))
  EXTERN_SRC := lpfc_hbadisc.c \
		lpfc_els.c \
		lpfc_ct.c \
		lpfc_mbox.c \
		lpfc_mem.c \
		lpfc_nportdisc.c \
		lpfc_sli.c \
		lpfc_scsi.c \
		lpfc_attr.c \
		lpfc_init.c
  SOURCEPATH ?= ../kernel/lpfc
else
  EXTERN_SRC :=
  SOURCEPATH :=
endif

clean-files := *.o *~ *.ko *.mod.o *.mod.c .*.cmd .*.flags .tmp_versions $(EXTERN_SRC)

ifeq (,$(STANDALONE))
%.c: $(SOURCEPATH)/%.c
	@cp $< ./ || chmod +w $@
	@cat $(SOURCEPATH)/lpfc_scsi.c \
	  | sed -e "s/^static *struct *lpfc_scsi_buf/struct lpfc_scsi_buf/" \
	        -e "s/^static *void/void/" \
	   > ./lpfc_scsi.c
	@cat $(SOURCEPATH)/lpfc_attr.c \
	  | sed -e "s/^static int/int/" \
	   > ./lpfc_attr.c
	@cat $(SOURCEPATH)/lpfc_els.c \
	  | sed -e "s/^static struct lpfc_iocbq/struct lpfc_iocbq/" \
	   > ./lpfc_els.c
	@cat $(SOURCEPATH)/lpfc_init.c \
          | sed -e "s/module_init(lpfc_init);//" \
                -e "s/module_exit(lpfc_exit);//" \
                -e "s/static int __init/int __init/" \
                -e "s/static void __exit/void __exit/" \
           > ./lpfc_init.c

endif

build: $(EXTERN_SRC)
	$(MAKE) -C $(BASEINCLUDE) SUBDIRS=`pwd` CONFIG_SCSI_LPFC_IOCTLS=m modules

install:
	install -c lpfcdfc.ko /lib/modules/$(KERNELVERSION)/kernel/drivers/scsi/$(lpioctls-module)
#	install -c $(lpfn-module) /lib/modules/$(KERNELVERSION)/kernel/drivers/net/$(lpfn-module)
	depmod -a

extern_src:  $(EXTERN_SRC)

clean:
#	$(MAKE) -C $(BASEINCLUDE) SUBDIRS=$(PWD) CONFIG_SCSI_LPFC=m CONFIG_NET_LPFC=m clean
	@rm -rf $(clean-files)
endif
